package com.ailanyin.admin.controller;

import com.ailanyin.admin.service.SysMenuService;
import com.ailanyin.admin.service.SysUserService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.model.SysMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 上午 11:24
 */
@RestController
@RequestMapping("/system/menu")
@Api(tags = "后台菜单管理")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserService sysUserService;

    @PostMapping
    @ApiOperation(value = "添加菜单")
    @PreAuthorize("hasAuthority('system:menu:add')")
    public AjaxResult insertMenu(@Validated @RequestBody SysMenu menu) {
        sysMenuService.insertMenu(menu);
        return AjaxResult.success();
    }


    @PutMapping
    @ApiOperation(value = "修改菜单")
    @PreAuthorize("hasAuthority('system:menu:edit')")
    public AjaxResult updateMenu(@Validated @RequestBody SysMenu menu) {
        sysMenuService.updateMenu(menu);
        return AjaxResult.success();
    }

    @DeleteMapping("/{menuId}")
    @ApiOperation(value = "删除菜单")
    @PreAuthorize("hasAuthority('system:menu:remove')")
    public AjaxResult removeMenu(@PathVariable("menuId") Long menuId) {
        sysMenuService.removeMenuById(menuId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "加载对应角色菜单列表树")
    @GetMapping(value = "/roleMenuTreeSelect/{roleId}")
    public AjaxResult roleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        List<SysMenu> menus = sysMenuService.selectMenuList(sysUserService.getCurrentUser().getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", sysMenuService.buildMenuTreeSelect(menus));
        return ajax;
    }

    @GetMapping("/treeSelect")
    @ApiOperation(value = "查询菜单下拉树")
    public AjaxResult treeSelect(SysMenu menu) {
        List<SysMenu> menus = sysMenuService.selectMenuList(menu, sysUserService.getCurrentUser().getUserId());
        return AjaxResult.success(sysMenuService.buildMenuTreeSelect(menus));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取菜单列表")
    @PreAuthorize("hasAuthority('system:menu:list')")
    public AjaxResult list(SysMenu menu) {
        List<SysMenu> menus = sysMenuService.selectMenuList(menu, sysUserService.getCurrentUser().getUserId());
        return AjaxResult.success(menus);
    }

    @GetMapping(value = "/{menuId}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    @ApiOperation(value = "根据菜单编号获取详细信息")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return AjaxResult.success(sysMenuService.selectMenuById(menuId));
    }
}
